// pages/add_car/add_car.js
var api = require('../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 省份简写
    provinces: [
      ['京', '沪', '粤', '津', '冀', '晋', '蒙', '辽', '吉', '黑'],
      ['苏', '浙', '皖', '闽', '赣', '鲁', '豫', '鄂', '湘'],
      ['桂', '琼', '渝', '川', '贵', '云', '藏'],
      ['陕', '甘', '青', '宁', '新'],
    ],
    // 车牌输入
    numbers: [
      ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
      ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K"],
      ["L", "M", "N", "P", "Q", "R", "S", "T", "U", "V"],
      ["W", "X", "Y", "Z", "港", "澳", "学"]
    ],
    carnum: [],
    showNewPower: false,
    KeyboardState: false,
    colors: [{
        name: "红色"
      },
      {
        name: "黑色"
      },
      {
        name: "白色"
      }
    ],
    car_types: [{
        name: "轿车"
      },
      {
        name: "SUV"
      },
      {
        name: "MPV"
      }
    ],
    id:'',
    type_index: 0,
    color_index: 0,
    selectBrand: {},
    selectCarType: '',
    selectCarColor: '',
    name: '',
    phone: '',
    is_default: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getTypeColor();
    if (options.id) {
      let editCar = wx.getStorageSync('editCar');
      if (editCar) {
        let plate_number = editCar.plate_number;
        let carnum = [];
        for (var i = 0; i < plate_number.length; i++) {
            carnum.push(plate_number.substr(i,1));
        }
        console.log(editCar)
        this.setData({
          carnum:carnum,
          plate_number:editCar.plate_number,
          name:editCar.name,
          phone:editCar.phone,
          selectCarColor:editCar.color,
          is_default:editCar.is_default ? true : false,
          'selectBrand.name':editCar.brand_name,
          'selectBrand.id':editCar.brand_id,
          'selectCarType':editCar.type,
          id:options.id,
        })
      }
    }
  },
  //获取车量信息
  getCarInfo() {

  },
  //选择车型
  click_car_brand() {
    wx.navigateTo({
      url: '../specific_models/specific_models?source=3',
    })
  },
  click_mask() {
    this.setData({
      show_type: false,
      show_color: false
    })
  },
  bind_confirm_type() {
    this.setData({
      show_type: false,
      selectCarType: this.data.car_types[this.data.type_index].name
    })
  },
  //颜色确定
  bind_confirm() {
    this.setData({
      show_color: false,
      selectCarColor: this.data.colors[this.data.color_index].name
    })
  },
  //选择类型
  select_type(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      type_index: index
    })
  },
  //选择颜色
  select_color(e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      color_index: index
    })

  },
  click_car_type() {
    this.setData({
      show_type: true
    })
  },
  //点击车身颜色
  click_car_color() {
    this.setData({
      show_color: true
    })
  },
  // 选中点击设置
  bindChoose(e) {

    if (!this.data.carnum[6] || this.data.showNewPower) {
      var arr = [];
      arr[0] = e.target.dataset.val;
      this.data.carnum = this.data.carnum.concat(arr)
      let KeyboardStateShow = true;
      if (this.data.carnum.length >= 7) {
        KeyboardStateShow = false;
      }
      this.setData({
        carnum: this.data.carnum,
        KeyboardState: KeyboardStateShow
      })
      console.log('输入 ', this.data.carnum.length)
      if (this.data.carnum.length >= 7) {
        this.KeyboardState = false;
      }
    }
  },
  bindDelChoose() {
    if (this.data.carnum.length != 0) {
      this.data.carnum.splice(this.data.carnum.length - 1, 1);
      this.setData({
        carnum: this.data.carnum
      })
    }
  },
  showPowerBtn() {
    this.setData({
      showNewPower: true,
      KeyboardState: true
    })
  },
  closeKeyboard() {
    this.setData({
      KeyboardState: false
    })
  },
  openKeyboard() {
    this.setData({
      KeyboardState: true
    })
  },
  //获取颜色类型
  getTypeColor() {
    api.get('/common/typeColor').then(res => {
      this.setData({
        car_types: res.types,
        colors: res.colors
      })
    })
  },
  changeCb() {
    this.setData({
      is_default: !this.data.is_default
    })
  },
  // 提交车牌号码
  submitNumber() {
    var that = this;
    if (that.data.carnum[6]) {
      // wx.showLoading();
      var number;
      if (!that.data.showNewPower) {
        if (that.data.carnum[6]) {
          number = that.data.carnum[0] + that.data.carnum[1] + that.data.carnum[2] + that.data.carnum[3] + that.data.carnum[4] + that.data.carnum[5] + that.data.carnum[6];
          that.addCarNumber(number);
        } else {
          wx.showToast({
            title: '请填写完整的车牌号码',
            icon: "none",
            duration: 2000
          })
        }
      } else {
        if (that.data.carnum[7]) {
          number = that.data.carnum[0] + that.data.carnum[1] + that.data.carnum[2] + that.data.carnum[3] + that.data.carnum[4] + that.data.carnum[5] + that.data.carnum[6] + that.data.carnum[7];
          that.addCarNumber(number);
        } else {
          wx.showToast({
            title: '请填写完整的车牌号码',
            icon: "none",
            duration: 2000
          })
        }
      }
    } else {
      wx.showToast({
        title: '请填写完整的车牌号码',
        icon: "none",
        duration: 2000
      })
    }
    if (this.data.carnum.length < 7) {
      wx.showToast({
        title: '请输入完整车牌号',
        icon: 'none'
      })
      return false;
    }
    if (!this.data.selectBrand.id) {
      wx.showToast({
        title: '请选择汽车品牌',
        icon: 'none'
      })
      return false;
    }
    if (!this.data.selectCarType) {
      wx.showToast({
        title: '请选择汽车类型',
        icon: 'none'
      })
      return false;
    }
    if (!this.data.selectCarColor) {
      wx.showToast({
        title: '请选择汽车颜色',
        icon: 'none'
      })
      return false;
    }
    if (!this.data.name) {
      wx.showToast({
        title: '请填写联系姓名',
        icon: 'none'
      })
      return false;
    }
    if (!this.data.phone) {
      wx.showToast({
        title: '请填写联系方式',
        icon: 'none'
      })
      return false;
    }
    api.post('/user/saveCar', {
      id:this.data.id,
      brand_id: this.data.selectBrand.id,
      type: this.data.selectCarType,
      plate_number: this.data.carnum.join(''),
      color: this.data.selectCarColor,
      name: this.data.name,
      phone: this.data.phone,
      is_default: this.data.is_default
    }).then(res => {
      if(this.data.is_default == true){
        wx.setStorageSync('defaultCar', res.car)
      }
      wx.navigateBack({
        delta: 1,
      })
    })


  },
  addCarNumber(number) {
    console.log(number); // 车牌号码
  },
  click_submit() {
    this.submitNumber()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})